package guinong.com.gnw.base;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import butterknife.ButterKnife;
import guinong.com.gnw.R;
import guinong.com.gnw.ui.widged.CustomProgressDialog;
import guinong.com.gnw.utils.L;
import guinong.com.gnw.utils.SharedPreferencesUtils;
import guinong.com.gnw.utils.T;


/**
 * Created by itte on 2016/4/14.
 */
public abstract class BaseFragment<P extends BasePresenter> extends Fragment {
    protected Context mContext;
    protected P mPresenter;
    protected SharedPreferencesUtils mSharedDataUtils;
    protected ViewGroup mRootView; //根视图
    protected Dialog progressDialog;
    /**
     * 标记是否可以显示Dilaog
     */
    private boolean isCanShowDialog = true;

    /**
     * 视图
     *
     * @return
     */
    protected abstract int inflateContentView();

    protected abstract void initData();//初始化数据

    protected void initPresenter() {
    }

    /**
     * \
     * 子类重写这个方法，初始化视图
     *
     * @param inflater
     * @param savedInstanceSate
     */
    protected abstract void intitView(LayoutInflater inflater, Bundle savedInstanceSate);

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (inflateContentView() > 0) {
            mRootView = (ViewGroup) inflater.inflate(inflateContentView(), null);
            mRootView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            ButterKnife.bind(this, mRootView);
            mContext = getActivity();
            mSharedDataUtils = SharedPreferencesUtils.getInstance(mContext);
            initPresenter();
            intitView(inflater, savedInstanceState);
            initData();
            return mRootView;
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            L.e("fragment:" + getClass().getSimpleName());
        }
    }

    /**
     * 通过Class跳转界面
     **/
    protected void startActivity(Class<?> cls) {
        startActivity(cls, null);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    protected void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 通过Action跳转界面
     **/
    protected void startActivity(String action, Uri uri) {
        Intent intent = new Intent(action, uri);
        startActivity(intent);
    }

    /**
     * 含有Bundle通过Action跳转界面
     **/
    protected void startActivity(String action, Bundle bundle) {
        Intent intent = new Intent();
        intent.setAction(action);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * toast
     *
     * @param msg
     */
    public void toastShortBasic(String msg) {
        T.showShort(mContext, msg);
    }

    public void toastLongBasic(String msg) {
        T.showLong(mContext, msg);
    }

    public void showLoading() {
        showLoading(getResources().getString(R.string.loading_normal_str));
    }

    /**
     * 对话框预留
     **/
    public void showLoading(String msg) {
        stopLoading();
        progressDialog = CustomProgressDialog.createDialog(mContext, msg);
        progressDialog.show();
    }

    /**
     * 对话框预留
     **/
    public void stopLoading() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroy();
        }
    }

}
